//
//  XLAppletManager.m
//  campus
//
//  Created by xl on 2020/1/13.
//  Copyright © 2020 xl. All rights reserved.
//

#import "XLAppletManager.h"
#import <TinyappService/TASUtils.h>
#import "XLWebManager.h"
#import "XLUserDefaults.h"
#import "UIDevice+XL.h"

@interface XLAppletManager ()

@end
@implementation XLAppletManager

+ (void)startMpassAppletAppId:(NSString *)appId  path:(NSString *)path reqStr:(NSString *)reqStr {
    //默认启动小程序都需要传token
    NSString *token = [XLUserDefaults getLastAvailableToken];
    
//    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
//    // app版本
//    NSString *appVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
//    //机器类型：iPhone
//    NSString * brand = [[UIDevice currentDevice] model];
//    //手机型号
//    NSString *model = [UIDevice currentDevice].machineType;
//    //手机系统版本：11.1.2
//    NSString* systemVersion = [[UIDevice currentDevice] systemVersion];
//    //系统 1、IOS ,2、安卓 ,3、其它
//    NSString *system = @"1";
//    //UniqueId
//    NSString *uniqueId = [UIDevice currentDevice].uuid;
    
    
    NSString *queryStr;
    if (ISEmptyString(reqStr)) {
        queryStr = [NSString stringWithFormat:@"token=%@", token];
    } else {
        queryStr = [NSString stringWithFormat:@"token=%@&%@", token, reqStr];
    }
    NSDictionary *param = @{@"page":path,@"query":queryStr};
    [DTContextGet() startApplication:appId params:param animated:YES];
}

+ (void)exitAllMpassAppletWithAppId:(NSString *)appId {
    [TASUtils exitTinyApplication:appId];
}

- (void)handleJumpLogic:(NSNumber *)showType
                    url:(NSString *)url
                  appId:(NSString *)appId {
    if (ISEmptyString(url)) {
        return;
    }
    NSArray *jumpArr = [url componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"?"]];
    if (jumpArr.count == 0) {
        return;
    }
    NSString *reqStr = (jumpArr.count > 1) ? jumpArr.lastObject:@"";
    if (showType.integerValue == 1) {//小程序跳转
        [XLAppletManager startMpassAppletAppId:appId path:jumpArr.firstObject reqStr:reqStr];
    } else if (showType.integerValue == 2) {//app内链跳转
    } else if (showType.integerValue == 3) {//H5内链跳转 mpass的h5容器打开
        [XLWebManager requestOPenWebUrl:url title:@""];
    } else if (showType.integerValue == 4) {//H5外链跳转
        if ([url containsString:@"https://"] || [url containsString:@"http://"]) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
        }
    }
}

@end
